package cn.sczc.jh.client.device;

import java.util.Map;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

import cn.sczc.jh.client.controller.WirelessDeviceAPIController;
import cn.sczc.jh.client.vo.DeviceInfoVo;
import cn.sczc.jh.client.vo.SensorOnlineVo;

@Service
public class JHDeviceInMQListener {
	private final Logger logger = LogManager.getLogger(this.getClass());
	@Autowired
	private WirelessDeviceAPIController wirelessDeviceAPIController;

	// 使用JmsListener配置消费者监听的队列，其中text是接收到的消息
	@JmsListener(destination = "jh.device.*.sensor.*.info", containerFactory = "jmsListenerFactory")
	public void receiveSensorInfo(byte[] data) {
		String text = StringEscapeUtils.unescapeJava(new String(data));
		DeviceInfoVo di = JSON.parseObject(text, DeviceInfoVo.class);
		try {
			logger.debug("接收传感器数据:{}", text);
			wirelessDeviceAPIController.sensorInfo(di.getDeviceId(), di.getData());
		} catch (Exception e) {
			logger.error("", e);
		}
	}

	@SuppressWarnings("unchecked")
	@JmsListener(destination = "jh.device.*.sensor.*.alarm", containerFactory = "jmsListenerFactory")
	public void receiveSensorAlarm(byte[] data) {
		String text = StringEscapeUtils.unescapeJava(new String(data));
		DeviceInfoVo di = JSON.parseObject(text, DeviceInfoVo.class);
		Map<String, Object> valueMap = (Map<String, Object>) di.getData();
		String sensorAddress = String.valueOf(valueMap.get("address"));
		try {
			logger.debug("接收传感器告警数据:{}", text);
			wirelessDeviceAPIController.sensorAlarm(di.getDeviceId(), sensorAddress, di.getData());
		} catch (Exception e) {
			logger.error("", e);
		}
	}

	@JmsListener(destination = "jh.device.*.sensor.*.offline", containerFactory = "jmsListenerFactory")
	public void receiveSensorOffline(byte[] data) {
		String text = StringEscapeUtils.unescapeJava(new String(data));
		SensorOnlineVo so = JSON.parseObject(text, SensorOnlineVo.class);
		try {
			logger.debug("设备传感器已下线 设备ID:{}", so.getDeviceId());
			wirelessDeviceAPIController.sensorOffline(so.getDeviceId(),so.getSensorAddress(),so);
		} catch (Exception e) {
			logger.error("", e);
		}
	}
	
	@JmsListener(destination = "jh.device.*.online", containerFactory = "jmsListenerFactory")
	public void receiveDeviceOnline(byte[] data) {
		String text = StringEscapeUtils.unescapeJava(new String(data));
		DeviceInfoVo di = JSON.parseObject(text, DeviceInfoVo.class);
		try {
			logger.debug("设备已上线 设备ID:{}", di.getDeviceId());
			wirelessDeviceAPIController.deviceOnline(di.getDeviceId());
		} catch (Exception e) {
			logger.error("", e);
		}
	}

	@JmsListener(destination = "jh.device.*.offline", containerFactory = "jmsListenerFactory")
	public void receiveDeviceOffline(byte[] data) {
		String text = StringEscapeUtils.unescapeJava(new String(data));
		DeviceInfoVo di = JSON.parseObject(text, DeviceInfoVo.class);
		try {
			logger.debug("设备已下线 设备ID:{}", di.getDeviceId());
			wirelessDeviceAPIController.deviceOffline(di.getDeviceId());
		} catch (Exception e) {
			logger.error("", e);
		}
	}

	@JmsListener(destination = "jh.device.*.info", containerFactory = "jmsListenerFactory")
	public void receiveDeviceInfo(byte[] data) {
		String text = StringEscapeUtils.unescapeJava(new String(data));
		DeviceInfoVo di = JSON.parseObject(text, DeviceInfoVo.class);
		try {
			logger.debug("接收设备数据:{}", text);
			wirelessDeviceAPIController.deviceInfo(di.getDeviceId(), di.getData());
		} catch (Exception e) {
			logger.error("", e);
		}
	}

	@JmsListener(destination = "jh.device.*.state", containerFactory = "jmsListenerFactory")
	public void receiveDeviceState(byte[] data) {
		String text = StringEscapeUtils.unescapeJava(new String(data));
		DeviceInfoVo di = JSON.parseObject(text, DeviceInfoVo.class);
		try {
			logger.debug("接收设备状态:{}", text);
			wirelessDeviceAPIController.deviceState(di.getDeviceId(), di.getData());
		} catch (Exception e) {
			logger.error("", e);
		}
	}

	@JmsListener(destination = "jh.device.*.port", containerFactory = "jmsListenerFactory")
	public void receiveDevicePort(byte[] data) {
		String text = StringEscapeUtils.unescapeJava(new String(data));
		logger.debug("接收设备端口:{}", text);
		DeviceInfoVo di = JSON.parseObject(text, DeviceInfoVo.class);
		try {
			wirelessDeviceAPIController.devicePorts(di.getDeviceId(), di.getData());
		} catch (Exception e) {
			logger.error("", e);
		}
	}

	@JmsListener(destination = "jh.device.*.cloudpush", containerFactory = "jmsListenerFactory")
	public void receiveCloudPushState(byte[] data) {
		String text = StringEscapeUtils.unescapeJava(new String(data));
		logger.debug("接收云端推送状态:{}", text);
		DeviceInfoVo di = JSON.parseObject(text, DeviceInfoVo.class);
		try {
			wirelessDeviceAPIController.cloudPushState(di.getDeviceId(), di.getData());
		} catch (Exception e) {
			logger.error("", e);
		}
	}
}
